<?php

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */
class Commercial_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    public function getAllAdvertising($number, $offset, $keyword) {
        $return = array();
        $this->db->select('*')
                ->from('advertising')
                ->like('name', $keyword)
                ->limit($number, $offset);
        $rs = $this->db->get();
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }
    
    public function getAdInfo($id)
    {
        $return = array();
        $this->db->select('*')
                ->from('advertising')
                ->where('id', $id);
        $rs = $this->db->get();
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }

    function count_all($keyword){
        if($keyword != ''){
            return $this->db->select('name')
                    ->from('advertising')
                    ->like('name', $keyword)
                    ->count_all_results();
        }
			return $this->db->count_all('advertising');
		}
    
    public function deleteCommercial($Id) {
        $this->db->delete('advertising', array('id' => $Id));
    }

     public function updateCommercialDeleteF($Id, $data = array()) {
        if (!is_null($data)) {
            $this->db->update('advertising', $data, array(
                'id' => $Id
            ));
        }
    }

    public function networkExists($name) {
        $this->db->select('*');
        $this->db->from('list_network');
        $this->db->where('name', $name);
        $rs = $this->db->get();
        $return = ($rs->num_rows() > 0) ? true : false;
        $rs->free_result();
        return $return;
    }
    
     public function updateCommercial($Id, $data = array()) {
        if (!is_null($data)) {
            $result = $this->db->update('advertising', $data, array(
                'id' => $Id,
            ));
            return true;
        }
        return false;
    }

    public function addCommercial($name, $upload, $Width, $Height, $href, $Location, $No) {
        $array = array(
            'name' => $name,
            'type'  =>  'image',
            'img' => $upload,
            'width' => $Width,
            'height' =>$Height,
            'href' => $href,
            'position' => $Location,
            'order' => $No,
            'status' => '1'
        );
        return $this->db->insert('advertising', $array);
    }

}

?>
